package com.sky.controller.admin;

import com.sky.properties.AliyunOSSProperties;
import com.sky.result.Result;
import com.sky.utils.AliyunOSSUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/admin/common")
@Slf4j
@Api(tags = "文件上传")
public class UploadController {

    @Autowired
    private AliyunOSSProperties aliyunOSSProperties;

    /**
     * 文件上传
     * @param file
     * @return
     * @throws Exception
     */
    @RequestMapping("/upload")
    public Result upload (MultipartFile file) throws Exception{
        String originalFilename = file.getOriginalFilename();

        byte[] bytes = file.getBytes();

        int index = originalFilename.lastIndexOf(".");

        String extName = originalFilename.substring(index);

        //调用阿里云工具类
        String url = AliyunOSSUtils.upload(aliyunOSSProperties.getEndpoint(), aliyunOSSProperties.getBucketName(), bytes, extName);

        return Result.success(url);
    }
}
